<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<button onclick="TestSockets()">点我</button>

<input type="text" id="box">

<script>

function TestSockets() {
    var socket = new WebSocket("ws://127.0.0.1:8765");
    //设置连接成功后的回调函数
     var val = document.getElementById('box').value;
     // 实例对象websocket的onopen属性，可以用来指定连接成功后的回调函数
    socket.onopen=function () {
        console.log("socket has been opened");
        var message = {
            content: val
        };
        message = JSON.stringify(message);
        socket.send(message);
    };
}

</script>
</body>
</html>